<script setup lang="ts">
import { ref } from "vue";
import tree from "./dept-tree.vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useLogic } from "./logic/logic";
import { Permiss } from "./logic/types";
import { hasAuth } from "@/router/utils";
import { useI18n } from "vue-i18n";

import {
  Search,
  RefreshLeft,
  Plus,
  EditPen,
  Delete,
  Edit
} from "@element-plus/icons-vue";

const { t } = useI18n();

defineOptions({
  name: "user-manage"
});
console.log("user page")
const queryFormRef = ref();

const {
  loading,
  dataList,
  columns,
  queryFormData,
  pagination,
  deptTreeList,
  deptTreeLoading,
  openDialog,
  openResetPwdDialog,
  onSearch,
  resetForm,
  handleDelete,
  onDeptTreeSelect
} = useLogic();
</script>
<template>
  <div class="flex justify-between">
    <tree
      ref="treeRef"
      class="min-w-[200px] mr-2"
      :treeData="deptTreeList"
      :treeLoading="deptTreeLoading"
      @tree-select="onDeptTreeSelect"
    />
    <div class="w-[calc(100%-200px)]">
      <el-form
        ref="queryFormRef"
        v-auth="Permiss.READ"
        :inline="true"
        :model="queryFormData"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
        @submit.prevent
      >
        <el-form-item :label="t('sys.userPage.searchForm.username') + '：'" prop="search">
          <el-input
            v-model="queryFormData.search"
            :placeholder="t('sys.userPage.searchForm.placeholder.username')"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item :label="t('sys.userPage.searchForm.status') + '：'" prop="enabled">
          <el-select
            v-model="queryFormData.enabled"
            :placeholder="t('sys.userPage.searchForm.placeholder.status')"
            clearable
            class="!w-[180px]"
          >
            <el-option :label="t('sys.userPage.statusOptions.enabled')" :value="true" />
            <el-option :label="t('sys.userPage.statusOptions.disabled')" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="Search"
            :loading="loading"
            @click="onSearch"
          >
            {{ t('buttons.search') }}
          </el-button>
          <el-button :icon="RefreshLeft" @click="resetForm(queryFormRef)">
            {{ t('buttons.reset') }}
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar :title="t('sys.userPage.pageTitle')" :columns="columns()" @refresh="onSearch">
        <template #buttons>
          <el-button
            v-auth="Permiss.ADD"
            type="primary"
            :icon="Plus"
            @click="openDialog()"
          >
            {{ t('buttons.add') }}
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            align-whole="center"
            showOverflowTooltip
            table-layout="auto"
            :loading="loading"
            :size="size"
            adaptive
            :data="dataList"
            :empty-text="hasAuth(Permiss.READ) ? t('sys.userPage.table.emptyText.noData') : t('sys.userPage.table.emptyText.noPermission')"
            :columns="dynamicColumns"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="onSearch"
            @page-current-change="onSearch"
          >
            <template #operation="{ row }">
              <el-button
                v-auth="Permiss.UPT_PWD"
                class="reset-margin"
                link
                type="warning"
                :size="size"
                :icon="Edit"
                @click="openResetPwdDialog(row)"
              >
                {{ t('sys.userPage.table.operation.resetPwd') }}
              </el-button>
              <el-button
                v-auth="Permiss.UPT"
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="EditPen"
                @click="openDialog(t('buttons.edit'), row)"
              >
                {{ t('sys.userPage.table.operation.edit') }}
              </el-button>
              <Auth :value="Permiss.DEL">
                <el-popconfirm
                  :title="t('sys.userPage.table.operation.confirmDelete', { username: row.username })"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin"
                      :disabled="row.username === 'admin'"
                      link
                      type="primary"
                      :size="size"
                      :icon="Delete"
                    >
                      {{ t('buttons.delete') }}
                    </el-button>
                  </template>
                </el-popconfirm>
              </Auth>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
